package com.accounting.client.frontend.product.handlers;

import com.accounting.client.frontend.common.CurrencyBox;
import com.accounting.client.remote.Services;
import com.accounting.client.remote.enumeration.WeightUnit;
import com.accounting.client.remote.obj.Product;
import com.accounting.client.remote.transfer.UpdateProductRequest;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.Util;
import corelib.client.delegate.Action;
import corelib.client.resource.CommonResources;
import corelib.client.ui.SimpleModal;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.form.DecimalBox;
import corelib.client.ui.widget.form.IntInputBox;
import corelib.client.ui.widget.form.LazyInitializer;
import corelib.client.ui.widget.form.SelectField;
import corelib.client.ui.widget.form.TextAreaField;
import corelib.client.ui.widget.form.TextField;
import corelib.client.ui.widget.form.fomatters.ValueFormatter;
import corelib.client.ui.widget.layout.RelativeColumn;

public class UpdateProductHandler extends SimpleDialogHandler {

	private Action action;
	private Product productInfo;

	private TextField nameField;
	private TextField cNameField;
	private CurrencyBox postageField;
	private DecimalBox weightField;
	private SelectField<WeightUnit> weightUnitField;
	private TextAreaField descriptionField;
	private TextField KeywordsField;
	private IntInputBox listOrderField;

	public UpdateProductHandler(Product info, Action action) {
		this.action = action;
		this.productInfo = info;
	}

	@Override
	public String getHeading() {
		return "更新" + Util.getString(this.productInfo.getCName());
	}

	@Override
	public Size getSize() {
		return new Size(500, 500);
	}

	@Override
	public Widget getContentPanel() {
		int lw = 100;
		int fw = 300;
		int gap = 12;

		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(20, Unit.PX);
		RelativeColumn col = new RelativeColumn();
		col.setRowSpacing(20);

		col.addRow()
		   .add(LazyInitializer.createLabel("产品英文名称"), lw)
		   .addSpace(gap)
		   .add(nameField = LazyInitializer.createTextBox("产品英文名称"), fw);

		col.addRow()
		   .add(LazyInitializer.createLabel("产品中文名称"), lw)
		   .addSpace(gap)
		   .add(cNameField = LazyInitializer.createTextBox("产品中文名称"), fw);
		
		col.addRow()
		   .add(LazyInitializer.createLabel("关键搜索字段"), lw)
		   .addSpace(gap)
		   .add(KeywordsField = LazyInitializer.createTextBox("关键搜索字段"),fw);

		col.addRow()
		   .add(LazyInitializer.createLabel("排序号码"), lw)
		   .addSpace(gap)
		   .add(listOrderField = new IntInputBox(), fw);
		
		col.addRow()
				.add(LazyInitializer.createLabel("重量"), lw)
				.addSpace(gap)
				.add(weightField = new DecimalBox(), lw)
				.addSpace(gap)
				.add(weightUnitField = LazyInitializer.createSelectBox(WeightUnit.All), fw - gap - lw);

		col.addRow()
		   .add(LazyInitializer.createLabel("邮费"), lw)
		   .addSpace(gap)
		   .add(postageField = new CurrencyBox(), fw);
		
		

		col.addRow().add(LazyInitializer.createLabel("产品描述"), lw + fw);
		col.addRow().add(descriptionField = LazyInitializer.createTextArea(),lw + gap + fw + 30);
		descriptionField.setHeight(100);

		weightUnitField.setNullable(true);
		weightUnitField.setOptionFormatter("请选择重量单位",
				new ValueFormatter<WeightUnit>() {
					@Override
					public String format(WeightUnit value) {
						return value.getCname();
					}
				});
		sync();
		panel.add(col.getPanel());
		return panel;
	}

	private void sync() {
		listOrderField.setInteger(null, false);
		nameField.setText(this.productInfo.getName(), false);
		cNameField.setText(this.productInfo.getCName(), false);
		KeywordsField.setText(this.productInfo.getKeywords(), false);
		descriptionField.setValue(this.productInfo.getDescription());
		postageField.setText(Util.getString(this.productInfo.getPostage(), ""),
				false);
		weightField.setText(Util.getString(this.productInfo.getWeight(), ""),
				false);
		weightUnitField
				.setSelectedOption(WeightUnit.GetInstance(this.productInfo
						.getWeightUnit()), false);
	}

	@Override
	public IsButton[] getButtons() {
		return new IsButton[] { new IsButton("完成",
				CommonResources.INSTANCE.accept16(), new WidgetListener() {
					@Override
					public void valueUpdated(Widget widget) {
						createProduct();
					}
				}) };
	}

	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}

	private void createProduct() {
		if (Util.isBlank(cNameField.getTrimmedText())) {
			cNameField.markError("请输入产品中文名称");
			return;
		}

		if (Util.isBlank(nameField.getTrimmedText())) {
			nameField.markError("请输入产品英文名称");
			return;
		}

		if (Util.isBlank(descriptionField.getTrimmedValue())) {
			descriptionField.markError("请输入产品描述");
			return;
		}

		if (Util.isBlank(weightField.getTrimmedText())) {
			weightField.markError("请输入产品重量");
			return;
		}

		if (weightUnitField.getSelectedOption() == null) {
			weightUnitField.markError("请选择产品重量单位");
			return;
		}

		if (Util.isBlank(postageField.getTrimmedText())) {
			postageField.markError("请选择产品估计邮费");
			return;
		}

		String passport = SessionManager.getPassport();
		String productId = this.productInfo.getProductId();
		UpdateProductRequest request = new UpdateProductRequest();

		request.setCName(cNameField.getTrimmedText());
		request.setName(nameField.getTrimmedText());
		request.setDescription(descriptionField.getValue());
		request.setPrice(Util.getDouble(postageField.getTrimmedText(), 0.0));
		request.setWeight(Util.getDouble(weightField.getTrimmedText(), 0.0));
		request.setKeywords(Util.getString(KeywordsField.getTrimmedText()));
		request.setWeightUnit(weightUnitField.getSelectedOption().getName());
		request.setListOrder(listOrderField.getInteger());

		SimpleModal.show("努力更新中...");
		Services.ProductService.UpdateProduct(passport, productId, request,
			new AsyncCallback<Boolean>() {
				@Override
				public void onFailure(Throwable caught) {
					SimpleModal.hide();
					ErrorHandler.handleServiceError(caught);
				}

				@Override
				public void onSuccess(Boolean result) {
					SimpleModal.hide();
					if (result == Boolean.TRUE) {
						closeDialog();
						if (action != null) {
							action.run();
						}
					}
				}
			}
		);
	}
}
